import java.util.LinkedList;

public class Expression extends LinkedList<NumSign> {

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        for(NumSign num : this){
            sb.append(num.getNum());
            sb.append(num.getSign());
        }
        return sb.toString();
    }

    public String getResult(){
        calMulandDiv();
        calAddandSub();
        return this.get(0).getStrNumber();
    }

    private void calMulandDiv(){
        for(int i = 0;i < this.size();i++){
            if(1 == this.size()){
                return ;
            }
            if(this.get(i).getSign()=='*'){
                double temp = this.get(i).getNum() * this.get(i + 1).getNum();
                this.get(i + 1).setNum(temp);
                this.remove(i);
                i = i - 1;
                continue;
            }
            if(this.get(i).getSign()=='/'){
                double temp = this.get(i).getNum() / this.get(i + 1).getNum();
                this.get(i + 1).setNum(temp);
                this.remove(i);
                i = i - 1;
                continue;
            }
        }

    }

    private void calAddandSub(){
        for(int i = 0;i < this.size();i++){
            if(1 == this.size()){
                return ;
            }
            if(this.get(i).getSign()=='+'){
                double temp = this.get(i).getNum() + this.get(i + 1).getNum();
                this.get(i + 1).setNum(temp);
                this.remove(i);
                i = i - 1;
                continue;
            }
            if(this.get(i).getSign()=='-'){
                double temp = this.get(i).getNum() - this.get(i + 1).getNum();
                this.get(i + 1).setNum(temp);
                this.remove(i);
                i = i - 1;
                continue;
            }
        }
    }
}
